<?php

require_once('PHPUnit/Autoload.php');

include_once(dirname(__FILE__) . "/start.php");

class benefitTest extends PHPUnit_Framework_TestCase
{
	protected function setUp() {
        $this->clearBenefit();
        $this->clearLikes();
    }

    protected function tearDown() {
        //$this->clearBenefit();
        //$this->clearLikes();
    }

	private function clearBenefit(){
		$sql = "delete from Benefit";
		$repo = Repository::getInstance();
		$repo->query($sql);
	}

	private function clearLikes(){
		$sql = "delete from Likes";
		$repo = Repository::getInstance();
		$repo->query($sql);
	}

	private function getMockBenefit(){
		$benefit = new Benefit();
        $benefit->attributes->name = "test name";
        $benefit->attributes->description = "test description";
        $benefit->attributes->startDateTime = 1317489671;
        $benefit->attributes->endDateTime = 1317689671;
        $benefit->attributes->country = "Argentina";
        $benefit->attributes->province = "CiudadAutonomaDeBuenosAires";
        $benefit->attributes->city = "Villa del Parque";
        $benefit->attributes->address = "simbron";
        $benefit->attributes->number = "3008";
        $benefit->attributes->company = "test comp";
        $benefit->attributes->idType = 2;
        $benefit->attributes->idCategory = 4;

        return $benefit;
	}

	public function testSaveBenefit()
    {
        $benefit = $this->getMockBenefit();
        $success = $benefit->save();

        $this->assertTrue($success);
    }

	public function testSaveDuplicateBenefit()
    {
        $benefit = $this->getMockBenefit();
        $success = $benefit->save();
        $this->assertTrue($success);

        $benefit = $this->getMockBenefit();
        $success = $benefit->save();
        $this->assertFalse($success);
    }

    public function testTopRank_38()
    {
    	$script = file_get_contents(dirname(__FILE__) . "/test_38_carga_y_ranking_10_beneficios.sql");
    	$repo = Repository::getInstance();
    	$repo->multyQuery($script);

    	$benefit = new Benefit();
    	$topRanked = $benefit->getTopRanked(null,null, null,null,5);

    	$this->assertTrue($topRanked[0]->attributes->id == 10);
    	$this->assertTrue($topRanked[1]->attributes->id == 9);
    	$this->assertTrue($topRanked[2]->attributes->id == 8);
    	$this->assertTrue($topRanked[3]->attributes->id == 7);
    	$this->assertTrue($topRanked[4]->attributes->id == 6);
    }

    public function testTopRank_39()
    {
    	$script = file_get_contents(dirname(__FILE__) . "/test_39_carga_y_ranking_3_beneficios.sql");
    	$repo = Repository::getInstance();
    	$repo->multyQuery($script);

    	$benefit = new Benefit();
    	$topRanked = $benefit->getTopRanked(null,null, null,null,5);

    	$this->assertTrue($topRanked[0]->attributes->id == 3);
    	$this->assertTrue($topRanked[1]->attributes->id == 2);
    	$this->assertTrue($topRanked[2]->attributes->id == 1);
    	$this->assertTrue(count($topRanked) == 3);

    }

    public function testTopRank_40()
    {

    	$script = file_get_contents(dirname(__FILE__) . "/test_40_carga_y_ranking_7_beneficios.sql");
    	$repo = Repository::getInstance();
    	$repo->multyQuery($script);

		// posicionar en Lat: -34.6176284 Long: -58.3685974
    	$benefit = new Benefit();
    	$topRanked = $benefit->getTopRanked(null,null, '-58.3685974','-34.6176284',20);


    	$this->assertTrue($topRanked[0]->attributes->id == 7);
    	$this->assertTrue($topRanked[1]->attributes->id == 6);
    	$this->assertTrue($topRanked[2]->attributes->id == 4);
    	$this->assertTrue($topRanked[3]->attributes->id == 5);
    	$this->assertTrue($topRanked[4]->attributes->id == 1);


    }
}
?>
